#pragma once

#include "DXAMLCommon.h"


extern UINT_PTR __security_cookie;

template<typename T> 
class EncodedPtrWithDelete;

template<typename T> 
class EncodedPtr 
{
	friend EncodedPtrWithDelete<T>;
public:

	EncodedPtr()
	{
		this->m_ValidMask = 0;
		this->m_pTarget = NULL;
		this->m_pUnencoded = NULL;
		this->m_uiEncodingCookie = 0;

	}

	void Set(T *pTarget)
	{
		XUINT32 validMask;

		if ( pTarget && __security_cookie )
			validMask = 0xFFFFFFFFu;
		else
			validMask = 0;
		this->m_ValidMask = validMask;
		this->m_pTarget = this->EncodeDecode(pTarget);
		this->m_pUnencoded = pTarget;
		this->m_uiEncodingCookie = __security_cookie;
	}

	bool IsValid()
	{
		return this->m_ValidMask != 0;
	}

	T * Get()
	{
		return (T *)(this->m_ValidMask & (XUINT32)this->EncodeDecode(this->m_pTarget));
	}

	T * operator->()
	{
		return this->Get();
	}


	void DebugVerifyCookie(XUINT32 uiCookie)
	{
		NOTIMPL
	}

private:
	void * EncodeDecode(void *pVal)
	{
		return (void *)((XUINT32)pVal ^ __security_cookie);
	}

	XUINT32 m_ValidMask;
	void *m_pTarget;
	T *m_pUnencoded;
	XUINT32 m_uiEncodingCookie;
};
